﻿/*************************************************************************/
/** Copyright.                                                          **/
/** FileName: Calibrate.c                                               **/
/** Author: kongls                                                      **/
/** Version: 1.0.0.0                                                    **/
/** Description: Calibrate for leq withou headfile                      **/
/*************************************************************************/
//=======================================================================//
//= Include files.                                                      =//
//=======================================================================//
#include "DemoProc.h"
#include "HMI_Engine.h"
#include "SGUI_Basic.h"

extern char time_str[64];
extern LEQ_DATA_EVENT_DATA  LeqData;
extern char battery_str[3];

//=======================================================================//
//= Macro definition.                                                   =//
//=======================================================================//

//=======================================================================//
//= Static function declaration.                                        =//
//=======================================================================//
static HMI_ENGINE_RESULT    HMI_Calibrate_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT    HMI_Calibrate_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT    HMI_Calibrate_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID);
static HMI_ENGINE_RESULT    HMI_Calibrate_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID);

//=======================================================================//
//= Static variable declaration.                                        =//
//=======================================================================//
HMI_SCREEN_ACTION       s_stCalibratePaintActions =     {   NULL,
                                                            HMI_Calibrate_Prepare,
                                                            HMI_Calibrate_RefreshScreen,
                                                            HMI_Calibrate_ProcessEvent,
                                                            HMI_Calibrate_PostProcess
                                                        };

static SGUI_UINT16      nCalibrateDefine = 0;  
//=======================================================================//
//= Global variable declaration.                                        =//
//=======================================================================//
HMI_SCREEN_OBJECT       g_stHMICalibratePaint =        {   HMI_SCREEN_ID_Calibrate_PAINT,
                                                            &s_stCalibratePaintActions
                                                       };

//=======================================================================//
//= Function define.                                                    =//
//=======================================================================//
HMI_ENGINE_RESULT HMI_Calibrate_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
    SGUI_Basic_ResetMask(pstDeviceIF);
    if(NULL != pstDeviceIF->fnClear)
    {
        pstDeviceIF->fnClear();
    }
    else
    {
        SGUI_Basic_DrawRectangle1(pstDeviceIF, 0, 0, pstDeviceIF->stSize.iWidth, pstDeviceIF->stSize.iHeight, SGUI_COLOR_BKGCLR, SGUI_COLOR_BKGCLR);
    }
    SGUI_Basic_ResetMask(pstDeviceIF);
    HMI_Calibrate_RefreshScreen(pstDeviceIF, pstParameters);

    printf("HMI_Calibrate_Prepare\n");
    return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_Calibrate_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
    printf("2 HMI_Calibrate_RefreshScreen\n");
    /*----------------------------------*/
    /* Variable Declaration             */
    /*----------------------------------*/
    SGUI_INT                iPaintBkgIdx;

    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
   
    // Paint background.
    // for(iPaintBkgIdx=0; iPaintBkgIdx<pstDeviceIF->stSize.iHeight; iPaintBkgIdx+=5)
    // {
    //     SGUI_Basic_DrawHorizontalLine(pstDeviceIF, 0, pstDeviceIF->stSize.iWidth-1, iPaintBkgIdx, SGUI_COLOR_FRGCLR);
    // }
    return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_Calibrate_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID)
{
    if(NULL != pstDeviceIF->fnClear)
    {
        pstDeviceIF->fnClear();
    }

    //printf("3 HMI_Calibrate_ProcessEvent id=%d\n", *piActionID);
    /*----------------------------------*/
    /* Variable Declaration             */
    /*----------------------------------*/
    HMI_ENGINE_RESULT           eProcessResult;
    SGUI_INT                    iProcessAction;
    KEY_PRESS_EVENT*            pstKeyEvent;
    char cLeq[8];

    /*----------------------------------*/
    /* Initialize                       */
    /*----------------------------------*/
    eProcessResult =            HMI_RET_NORMAL;
    iProcessAction =            HMI_DEMO_PROC_NO_ACT;
    pstKeyEvent =               (KEY_PRESS_EVENT*)pstEvent; // (KEY_PRESS_EVENT*)pstEvent;
  
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, time_str, &SGUI_DEFAULT_FONT_16, 1, 1, 80, SGUI_DRAW_NORMAL);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, battery_str, &SGUI_DEFAULT_FONT_16, 91, 1, 127, SGUI_DRAW_NORMAL);

    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Calibrate", &SGUI_DEFAULT_FONT_16, 1,  25, 90, SGUI_DRAW_NORMAL);
    if(0 == nCalibrateDefine)
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "94.0 dB SPL", &SGUI_DEFAULT_FONT_16, 1,  48, 120, SGUI_DRAW_NORMAL);
    else
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "93.8 dB SPL", &SGUI_DEFAULT_FONT_16, 1,  48, 120, SGUI_DRAW_NORMAL);
    
    snprintf(cLeq,sizeof(cLeq), "%.1f", (float)LeqData.uLeqA / 10);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, cLeq,   &SGUI_DEFAULT_FONT_16, 30, 70, 90,  SGUI_DRAW_NORMAL);

    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Sense(mV/Pa)", &SGUI_DEFAULT_FONT_16, 1,  105, 120, SGUI_DRAW_NORMAL);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "50.00",   &SGUI_DEFAULT_FONT_16, 30, 128, 100,  SGUI_DRAW_NORMAL);


    if(EVENT_ID_KEY_PRESS == pstEvent->iID)
    {
        printf("%s:%d EVENT_ID_KEY_PRESS keyvalue=%d\n", __FILE__, __LINE__, pstKeyEvent->Data.uiKeyValue);

        switch(pstKeyEvent->Data.uiKeyValue)
        {
            case KEY_VALUE_HOME:
            {
                // Event data is invalid.
                eProcessResult = HMI_RET_INVALID_DATA;
                break;
            }
            case KEY_VALUE_MENU:
            {
                // go to main menu
                printf("menu\n");
                iProcessAction = HMI_DEMO_PROC_CANCEL;
                break;
            }
            case KEY_VALUE_PAUSE:
            {
                // start calibrate
                printf("start calibrate\n");
                break;
            }
            case KEY_VALUE_ENTER:
            {
                // change spl 94 93.8dB
                nCalibrateDefine = !nCalibrateDefine;
                if(0 == nCalibrateDefine)
                    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "94.0 dB SPL", &SGUI_DEFAULT_FONT_16, 1,  48, 120, SGUI_DRAW_NORMAL);
                else
                    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "93.8 dB SPL", &SGUI_DEFAULT_FONT_16, 1,  48, 120, SGUI_DRAW_NORMAL);
    
                printf("spl 94 93.8dB\n");
                break;
            }
            default:
            {
                break;
            }
        }
    }

    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/

    if(NULL != piActionID)
    {
        *piActionID = iProcessAction;
    }
    return eProcessResult;
}

HMI_ENGINE_RESULT HMI_Calibrate_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID)
{
    //printf("4 HMI_Calibrate_PostProcess id=%d\n\n", iActionID);
    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
    if(HMI_PROCESS_SUCCESSFUL(eProcResult))
    {
        if(HMI_DEMO_PROC_CANCEL == iActionID)
        {
            HMI_GoBack(NULL);
        }
    }

    return HMI_RET_NORMAL;
}



